package com.Web.Service.Impl;

import com.Web.DAO.IUserDAO;
import com.Web.DAO.Impl.UserDAOImpl;
import com.Web.Person.User;
import com.Web.Service.IUserService;
import com.Web.util.PageInfo;

import java.util.List;

public class UserServiceImpl implements IUserService {

    private IUserDAO userDAO = new UserDAOImpl();

    @Override
    public User login(String name, String password) {
        User user = userDAO.login(name, password);
        return user;
    }

    @Override
    public PageInfo<User> selectByPage(int pageNo, int pageSize) {
        int offset = (pageNo - 1) * pageSize;
        List<User> list = userDAO.selectByPage(offset, pageSize);
        int totalCount = userDAO.selectTotalCount();
        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
        PageInfo pageInfo = new PageInfo(list, totalPage, pageNo, pageSize);
        return pageInfo;
    }

    @Override
    public void deleteById(int id) {
        userDAO.deleteById(id);
    }

    @Override
    public void add(User user) {
        userDAO.add(user);
    }

    @Override
    public User selectById(int id) {
        return userDAO.selectById(id);
    }

    @Override
    public void update(User user) {
        userDAO.update(user);
    }
}
